<?php
namespace app\dsadmin\controller;
use app\common\model\EnameModel;
use app\dsadmin\model\EnamecdnModel;
use think\Db;
use think\Cache;
class Ename extends Base
{
    public function index()
    {
    //   $list = Db::name('member')->where(true)->select();
     
    
    //  foreach ($list as $v){
           
    //       $userid = $v['id'];
    //     //   $info = Db::name('siyou')->where(['userid' => $v['id']])->find();
        
       
          
    //       $res = Db::name('pian')->where(['userid' => $userid])->find();
    //       if($res){
             
    //         //   $money = 4.99;
             
    //         //   Db::name('pian')->insert(['userid' => $v['id'],'money' => $money,'time' => time()]);
    //           Cache::store('redis')->set("money".$userid,$res['money'],77879680);
    //       }
        
    //         // Db::name('member')->where(['id' => $userid])->update(['zftd' => 1]);
        
            
    //         //  $member = Db::name("member")->where(['id' => $userid])->find();
            
    //         //  Cache::store('redis')->set("member".$userid,json_encode($member),77879680);
           
        
        
   
    //  }
     
    
    //  $list = Db::name('siyou')->where(['photo' => ['neq','']])->limit(20000,40000)->order('id desc')->select();
     

    //     foreach($list as $v){
    //         $url = $v['photo'];
            
         
          
           
           
    //         //正则表达式
    //         $reg = '/(http):\/\/([^\/]+)/i';
    //         preg_match($reg, $url,$res);

    //         /**  $res的结果

    //         */
    //         //要替换的位置替换成什么
    //         $url1 = 'sssppp1.amghtmp.cn';
            
    //         $url2 =  preg_replace($reg,'http://'.$url1, $url);

          
    //         Db::name('siyou')->where(['id' => $v['id']])->update(['photo' => $url2]);

    //     }
    //     die;
        
        $list =  Db::name('member')->where(['ename_first' => ['neq','']])->select();
        
        foreach ($list as $v){
            
            Db::name('ename')->where(['ename' => $v['ename_first']])->update(['daili_id' => $v['id']]);
            
        }
        
        $list =  Db::name('member')->where(['ename_two' => ['neq','']])->select();
        
        foreach ($list as $v){
            
            Db::name('ename')->where(['ename' => $v['ename_two']])->update(['daili_id' => $v['id']]);
            
        }
        
        
        $key  = input('key');
        $map = [];
        if ($key && $key !== "")
        {
            $map['e.ename'] = ['like', "%" . $key . "%"];
        }
        $Nowpage = input('get.page') ? input('get.page'):1;
        $limits = 100; // 获取总条数
        $count = Db::name('ename')->where($map)->count(); //计算总页面
        $allpage = intval(ceil($count / $limits));
        $ename = new EnameModel();
        $lists = $ename->getEnamesByWhere($map, $Nowpage, $limits);
        $daili = Db::name('member')->where(['status' => 1 ])->select();
        
       
        foreach($lists  as &$v){
            
            if(!empty($v['luodi'])){
                $v['luodi'] = count(json_decode($v['luodi']));
            }else{
                $v['luodi'] = 0;
            }

            if($v['type'] == 1){

                 $b = '';
               
                $first = Db::name('member')->where(['ename_first' => $v['ename'],'status' => 1])->select();
                if(count($first) > 1){

                    foreach($first as $k){
                        $b .= "【".$k['account'].'】';
                    }

                    $v['account'] = $b;

                }
            }

            if($v['type'] == 2){
                $c = '';
                $two = Db::name('member')->where(['ename_two' => $v['ename'],'status' => 1])->select();
               
                if(count($two) > 1){

                    foreach($two as $j){
                        $c .= "【".$j['account'].'】';
                    }
                    $v['account'] = $c;

                }

            }

        }

        $this->assign('daili', $daili);
        $this->assign('Nowpage', $Nowpage); //当前页
        $this->assign('allpage', $allpage); //总页数 
        $this->assign('val', $key);
        if (input('get.page'))
        {
            return json($lists);
        }
        return $this->fetch();
    }


   

    
    public function jcsite_oneurl_luodi()
    {
          $map['status'] = 1;
          $map['zt']     = 1;
          $map['type']   = 3;
          $num_luodi = input('num_luodi',0);
         
          
          if(empty($num_luodi)){
              $num_luodi = 0;
          }
          $ename = Db::name('ename')->where($map)->select();
        if (request()->isAjax())
        {
            
           
            header("Content-Type: text/html; charset=utf-8");
 
            if(count($ename) > 0)
            {
                $url  = $ename[$num_luodi]['ename'];
                $id   = $ename[$num_luodi]["id"];
                
                $url2 = file_get_contents("http://wx.03426.com/api.php?token=1c89fda6b42f06029f4330d6f5ddd319&url={$url}&type=1");
                // $url2 = file_get_contents("http://vip.xxweixin.com/weixin/wx_domain.php?user=jc2173835712&key=31e66d31a35c6ac835cd548e07cec445&domain={$url}");
                $json = json_decode($url2, true);
                
                if($json['code'] == 137){
                   return json(['id' => $id, 'state' => 66, 'msg' => '刷新频率过快', 'zt' => 1]);
                }
                
                
            
                // echo json_encode($json);
                if ($json['msg'] == '域名正常' && $json['code'] == 200)
                {
                    $flag = Db::name('ename')->where('ename',$url )->setField(['status' => 1, 'zt' => 1]);
                    return json(['id' => $id, 'state' => 66, 'msg' => $url.'正常']);
                }
                
                if($json['msg'] == '域名已被封' && $json['code'] == 201){
                    $flag = Db::name('ename')->where('ename',$url)->setField(['status' => 0, 'zt' => 0]);
                    return json(['id' => $id, 'state' => 88, 'msg' => $url.'封禁']);
                }
                
            }
            else
            {
                return json(['msg' => '域名用光了']);
            }
        }
        $this->assign('num', count($ename));
        return $this->fetch();
    }
    
     public function jcsite()
    {
        
        if (request()->isAjax())
        {
            header("Content-Type: text/html; charset=utf-8");
            $param = input('post.');
            $url   = $param['url'];
            $id    = $param["id"];
            $key   = "ms0Xao0VUZGFYCGSxx@aaa";
            $url2  = file_get_contents("http://wx.03426.com/api.php?token=6cf020d0efa93faf3faf6533a6db4ffd&url={$url}&type=1");
            $json  = json_decode($url2, true);
            
            
            if($json['code'] == 137){
                 return json(['id' => $id, 'state' => 66, 'msg' => '刷新频率过快', 'zt' => 1]);
            }
        
            //{"url":"safe","err":""}
            // $json['id'] = $id; 
            if ($json['msg'] == '域名正常' && $json['code'] == 200)
            {
                $flag = Db::name('ename')->where('ename', $url)->setField(['status' => 1, 'zt' => 1]);
                return json(['id' => $id, 'state' => 66, 'msg' => '正常', 'zt' => 1]);
            }
            
            if($json['msg'] == '域名已被封' && $json['code'] == 201){
                
                $flag = Db::name('ename')->where('ename',$url)->setField(['status' => 0, 'zt' => 0]);
                return json(['id' => $id, 'state' => 88, 'msg' => $url.'封禁']);
                
            }
        }
    }
    
    public function add()
    {
        if (request()->isAjax())
        {
            $param = input('post.');
            $ename = new EnameModel();
            $flag = $ename->insertEname($param);

            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
        }

        return $this->fetch();
    }
    
       public function zadd()
    {
        if (request()->isAjax())
        {
            $param = input('post.');
            $ename = new EnamecdnModel();
            $flag = $ename->insertEname($param);

            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
        }

        return $this->fetch();
    }

    public function edit()
    {
        $ename = new EnameModel();

        if (request()->isAjax())
        {
            $param = input('post.');
            
            $ename  = $param['ename'];
                
            $fangfeng = Db::name('ename')->where(['ename' => $ename])->find();
            
            $fangfeng_type = $fangfeng['fangfeng_type'];
            
            
            $fangfeng['ename_type'] = 1;
            
            //http://120.79.88.48/
            if($fangfeng_type == 666){
                $json_url = "http://120.26.164.1:1666/index/index/zhuyumin";
            }
            
            $json_data['luodi']       = $param['luodi'];
            $json_data['secretKey']   = "e10adc3949ba59abbe56e057f20f883e";
            $json_data['mainDomain']  = $ename;
            $json  = json_encode($json_data);
            
            try
            {
                $res   = get_duan_url($json_url,$json);
            
                $res_r = json_decode($res,true);
                
                
                if($res_r['status'] == 200){
                    $enameadd = Db::name('ename')->where(['ename' => $ename])->insert(['type'=>1,'ename'=>$param['luodi'],'fangfeng_type'=>$fangfeng_type,'status'=>1,'ename_type' => $fangfeng['ename_type']]);
                    
                    if($enameadd){
                        $member = Db::name('member')->where(['ename_first' => $ename])->select();
                        if(!empty($member)){
                            foreach ($member as $value) {
                                Db::name('member')->where(['ename_first' => $value['ename_first']])->update(['ename_first' => $param['luodi']]);
                            }
                        }
                    }
                    //香港DWZ47.57.143.193
                    $json_urls1 = "http://dwz.muhasq.cn/api/dwz_names?dwzurl=$ename&dwzurl_1=".$param['luodi'];
                    
                    //深圳DWZ119.23.50.79
                    $json_urls2 = "http://gtm-cn-tl32sco590k.gtm-a1b5.com/api/dwz_names?dwzurl=$ename&dwzurl_1=".$param['luodi'];
                     
                    //杭州DWZ120.26.164.1
                    $json_urls3 = "http://alb-oq96v10bka429jenba.cn-hangzhou.alb.aliyuncs.com/api/dwz_names?dwzurl=$ename&dwzurl_1=".$param['luodi'];
                    
                    //api短网址
                    $json_urls4 = "http://0720.okeytag.com/api/replace.php?id=$ename&url=".$param['luodi'];
                     
                    $json_datas['dwzurl']       = $ename;
                    $json_datas['dwzurl_1']     = $param['luodi'];
                    $jsons = json_encode($json_datas);
                    get_duan_url($json_urls1,$jsons);
                    get_duan_url($json_urls2,$jsons);
                    get_duan_url($json_urls3,$jsons);
                    file_get_contents($json_urls4);
                    
                    return ['code' => 1, 'data' => '', 'msg' => "修改成功"];
                    
                }else{
                    return ['code' => 0, 'data' => '', 'msg' => "防封中找不到该域名，请先检查防封中是否有！！！"];
                }
            }catch(PDOException $e){
                
                return ['code' => 0, 'data' => '', 'msg' => $e->getMessage()];
            }
        }

        $daili = Db::name('member')->where(['status' => 1 ])->select();
        
        $id = input('param.id');
        $ename = $ename->getOneEname($id);

        $this->assign([
            'daili' => $daili,
            'ename' => $ename,
            ]);
        return $this->fetch();
    }
    
    
    
    public function zedit()
    {
        $ename = new EnamecdnModel();

        if (request()->isAjax())
        {
            $param = input('post.');
           
            $flag = $ename->editEname($param);

            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
        }

        $id = input('param.id');
        
        $ename = $ename->getOneEname($id);
        
        $this->assign([
           
            'ename' => $ename,
            ]);
        return $this->fetch();
    }

    public function del()
    {
        $id = input('param.id');
        $info = Db::name('ename')->where(['id' => $id])->find();
        if($info['type'] == 1){
            return json(['code' => 0, 'data' => '', 'msg' => "删除失败"]);
        }
        $role = new EnameModel();
        $flag = $role->delEname($id);
        return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
    }
    
    public function zdel()
    {
        $id = input('param.id');
        $role = new EnamecdnModel();
        $flag = $role->delEname($id);
        return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
    }

    public function del_wxdanger()
    {
        $role = new EnameModel();
        $flag = $role->delEname_wxdanger();
        return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
    }

    public function status()
    {
        $id = input('param.id');
        $info = Db::name('ename')->where('id', $id)->find();
        $status = $info['status']; //判断当前状态情况
        if ($status == 1)
        {
            $flag = Db::name('ename')->where('id', $id)->setField(['status' => 0]);
            $url = Db::name('ename')->where(['status' => 1, 'zt' => 1,'type' => 3])->value('ename');
            Cache::store('redis')->set('ylluodi',$url,864000);
            return json(['code' => 1, 'data' => 1, 'msg' => '1']);
        }
        else
        {
            $flag = Db::name('ename')->where('id', $id)->setField(['status' => 1]);
            $url = Db::name('ename')->where(['status' => 1, 'zt' => 1,'type' => 3])->value('ename');
            Cache::store('redis')->set('ylluodi',$url,864000);
            return json(['code' => 0, 'data' => 0, 'msg' => '2']);
        }
    }
    
    
      public function zstatus()
    {
        $id = input('param.id');
        $info = Db::name('enamecdn')->where('id', $id)->find();
        $status = $info['status']; //判断当前状态情况
      
        if ($status == 1)
        {//关闭
            $flag = Db::name('enamecdn')->where('id', $id)->setField(['status' => 0]);
            $info = Db::name('enamecdn')->where(['status' => 1])->order('id asc')->find();
           
            $url   = $info['ename'];
          
            
            Cache::store('redis')->set('tsimage_urlz',$url,864000);
            
            Cache::store('redis')->set('ylimage_urlz',$url,864000);
            
            Db::name('config')->where(['name' => 'image_urlz'])->update(['value' => $url]);
             
            return json(['code' => 1, 'data' => 1, 'msg' => '1']);
        }
        else
        {//开启
            $flag = Db::name('enamecdn')->where('id', $id)->setField(['status' => 1]);
            
            $info = Db::name('enamecdn')->where('id', $id)->find();
            
            $url   = $info['ename'];
          
            
            Cache::store('redis')->set('tsimage_urlb',$url,864000);
            
            Cache::store('redis')->set('ylimage_urlz',$url,864000);
            
            Db::name('config')->where(['name' => 'image_urlz'])->update(['value' => $url]);
            
            return json(['code' => 0, 'data' => 0, 'msg' => '2']);
        }
    }
    
   //图片域名
   
   public function zindex(){
     
        $key  = input('key');
        $map = [];
        if ($key && $key !== "")
        {
            $map['e.ename'] = ['like', "%" . $key . "%"];
        }
        $Nowpage = input('get.page') ? input('get.page'):1;
        $limits = 100; // 获取总条数
        $count = Db::name('enamecdn')->where($map)->count(); //计算总页面
        $allpage = intval(ceil($count / $limits));
        $ename = new EnamecdnModel();
        $lists = $ename->getEnamesByWhere($map, $Nowpage, $limits);
     
        $this->assign('Nowpage', $Nowpage); //当前页
        $this->assign('allpage', $allpage); //总页数 
        $this->assign('val', $key);
        if (input('get.page'))
        {
            return json($lists);
        }
        return $this->fetch();
       
   }

}
